/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI;

import Model.*;
import javax.microedition.lcdui.*;

/**
 *
 * @author Pisarchik_S
 */
public class ShowStatistics implements CommandListener{


    Dispatcher m_Dispatcher;
    CModel m_Model;

    public Command [] m_Comands;

    public Command m_ShowMainMenu = new Command("Мэню", Command.CANCEL, 0);

    public ShowStatistics(Dispatcher dispatcher, CModel model)
    {
        m_Dispatcher = dispatcher;
        m_Model = model;
    }

    public void Init()
    {
        Form form  = new Form("Статыстыка");
        int totalAmount = 0;
        for (int i =0; i < m_Model.m_Records.length; ++i)
            totalAmount += m_Model.m_Records[i].m_nAmount;

        StringItem si = new StringItem("Агульныя траты: ", Integer.toString(totalAmount));
        si.setPreferredSize(400, 10);
        form.append(si);

        si = new StringItem("Па катэгорыях ", "");
        si.setPreferredSize(400, 10);
        form.append(si);

        for (int i = 0; i< m_Model.m_Categories.length; ++i)
        {
            CCategory cat = m_Model.m_Categories[i];
            int amount = 0;
            for (int j = 0; j < m_Model.m_Records.length; ++j)
                if (m_Model.m_Records[j].m_category == cat)
                {
                    amount += m_Model.m_Records[j].m_nAmount;
                }
            if (amount == 0)
                continue;
            si = new StringItem(cat.GetName() + ": ", Integer.toString(amount));
            si.setPreferredSize(400, 10);
            form.append(si);
        }

        form.addCommand(m_ShowMainMenu);
        form.setCommandListener(this);

        m_Dispatcher.GetDisplay().setCurrent(form);
    }

     public void commandAction(Command c, Displayable s)
    {
        if (c == m_ShowMainMenu)
        {
            m_Dispatcher.GetMainMenuEditor().Init();
        }
    }

}
